package com.budwk.app.sys.commons.task.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.fetchdata.models.FetchData_emergency;
import com.budwk.app.fetchdata.services.FetchdataEmergencyService;
import com.budwk.app.miniapp.utils.HttpUtils;
import com.budwk.starter.email.EmailSendServer;
import com.budwk.starter.job.annotation.SJob;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Slf4j
@IocBean(name = "pullTrafficRoadService",singleton = false)
public class PullTrafficRoadService {
    @Inject
    private FetchdataEmergencyService wxSpiderEmergencyInformationService;

    @Inject
    private EmailSendServer emailSendServer;

    @Inject("taskQueryExecutor")
    private ThreadPoolExecutor taskQueryExecutor;
    @Inject
    private PropertiesProxy propertiesProxy;




     @SJob("trafficblocknews")
    public void trafficblocknews(String taskId, String params) {

        Date day2 = new Date();
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        day2.setHours(day2.getHours() - 120);

              System.out.println("贵州省交通运输厅: "+new Date());
        int pageNumber=1, pageSize=50;
        List<FetchData_emergency> warningBoList = new ArrayList<>();
        while (true) {
            Map<String, Object> params1 = new HashMap<>();
            params1.put("pageNumber", pageNumber);
            params1.put("pageSize", pageSize);
            String result = HttpUtils.getRequest("http://140.210.211.155:8089/datapi/trafficroad/trafficblocknews", params1);
            JSONObject jsonObject = JSONObject.parseObject(result);
            JSONObject dataJson = jsonObject.getJSONObject("data");
            JSONObject pagerJson = dataJson.getJSONObject("pager");
            int pageCount = pagerJson.getInteger("pageCount");
            if (pageNumber > pageCount) {
                break;
            }
            JSONArray listJson = dataJson.getJSONArray("list");
            Timestamp start =Timestamp.valueOf(sdf2.format(day2));
            JSONArray listJsonNew =  listJson.stream().filter(item->((Timestamp)((JSONObject)item).getTimestamp("save_time")).compareTo(start)>0)
                    .collect(Collectors.toCollection(JSONArray::new));
            if(listJsonNew.isEmpty()) {
                pageNumber++;
                continue;
            }
            for (int i = 0; i < listJson.size(); i++) {
                JSONObject item = listJson.getJSONObject(i);
                FetchData_emergency warningBo = new FetchData_emergency();
                warningBo.setInformationtitle(item.getString("f_20216323178_ext"));//标题
                if(warningBo.getInformationtitle()==null)
                {
                    warningBo.setInformationtitle(item.getString("f_20216323178"));//标题
                    System.out.println(item);
                }
                if(warningBo.getInformationtitle()==null)
                {
                    System.out.println(item);
                }
                warningBo.setReleasetime(((Timestamp) item.getTimestamp("save_time"))); //发布时间




                FetchData_emergency vo = wxSpiderEmergencyInformationService.fetch(Cnd.where(FetchData_emergency::getInformationtitle,"=",warningBo.getInformationtitle())
                        .and(FetchData_emergency::getReleasetime,"=",warningBo.getReleasetime()));
                if (ObjectUtil.isEmpty(vo)) {
                    try {

                        warningBo.setInformationcontent(item.getString("f_202163172887").replaceAll(" ", ""));//内容
                        warningBo.setInformationchannels(item.getString("f_202163599791_ext"));//信息来源

                        warningBo.setProvince("贵州省");
                        warningBo.setDisaster_address("贵州省");
                        warningBo.setInformationmedia("贵州省交通运输厅");
                        warningBo.setDisaster_status("灾害");

                        warningBo.setDisaster_type("阻断信息");
                        //没有类型的再处理
                        warningBo.setPicurl("other.png");
                        warningBo.setAddperson("祥云AI");
                        warningBo.setAddphone("15555555555");
                        //添加人等
                        Date day = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        warningBo.setAddtime(sdf.format(day));
                        warningBo.setIsshow("1");//不显示
                        warningBoList.add(warningBo);
                    } catch(Exception e) {
                        e.printStackTrace();
                    }

                }
            }
            pageNumber++;
        }
        if(CollectionUtil.isNotEmpty(warningBoList)){
            wxSpiderEmergencyInformationService.fastInsert(warningBoList);
        }
    }
}
